/*
 * Copyright (c) 2009 Marius Mathiesen <marius.mathiesen@gmail.com>
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
 */
package no.trafikanten;

import java.util.Date;
import java.util.Calendar;
public class Departure 
{
	
	private int directionNumber;
	private String destinationName, departureTimeString, lineNumber;
	private Calendar departureTime;
	public static final int TRAM = 0;
	public static final int BUS = 1;
	public static final int SUBWAY = 2;
	public static final int BOAT = 3;
	
	public Departure() {
		departureTime = Calendar.getInstance();
	}
	
	public void setLineNumber(String lineNumber)
	{
		this.lineNumber = lineNumber;
	}
	
	public int getTransportationType()
	{
		if (this.lineNumber == "F3")
			return BUS;
		int lineN = this.getLineNumber();
		if (lineN <= 6)
			return SUBWAY;
		else if (lineN < 20)
			return TRAM;
		else if (lineN == 601 || lineN == 716)
			return BOAT;
		else return BUS;
	}
	
	public String getTransportationName()
	{
		switch (getTransportationType())
		{
		case (SUBWAY):
			return "subway";
		case TRAM:
			return "tram";
		case BOAT:
			return "boat";
		default:
			return "bus";
		}
	}
	
	public int getLineNumber()
	{
		return Integer.parseInt(this.lineNumber);
	}
	public void setDestinationName(String destinationName)
	{
		this.destinationName = destinationName;
	}
	public String getDestinationName()
	{
		return this.destinationName;
	}
	public String getTimeString()
	{
		return departureTimeString;
	}
	public void setDirectionNumber(int number)
	{
		this.directionNumber = number;
	}
	public String getDirectionNumberAsString()
	{
		return Integer.toString(this.directionNumber);
	}
	public int getDirection()
	{
		return directionNumber;
	}
	public int getDepartureNumber()
	{
		return getLineNumber();
	}
	public void setExpectedDeparture(Date departureTime )
	{
		this.departureTime.setTime(departureTime);
	}
	public Date getExpectedDeparture()
	{
		return this.departureTime.getTime();
	}
	public static String getDateString(Date departureTime_, Date now)
	{
		if (departureTime_.before(now)) return "";
		Calendar departureTime = Calendar.getInstance();
		departureTime.setTime(departureTime_);
		long millisecsDifference = departureTime_.getTime() - now.getTime();
		int minutesDifference = Math.round((millisecsDifference+15000) / (1000*60));
		if (minutesDifference < 1)
		{
			return "nå";
		}
		else if (minutesDifference < 10)
		{
			return minutesDifference + " min";
		}
		else
		{
			int mins = departureTime.get(Calendar.MINUTE);
			String minuteString;
			if (mins < 10)
				minuteString = "0" + mins;
			else 
				minuteString = Integer.toString(mins);
			
			return departureTime.get(Calendar.HOUR_OF_DAY) + ":" + minuteString;
		}
	}
	public String toDateString(Date now)
	{
		Date departureTime = getExpectedDeparture();
//		departureTime.setSeconds(0);
		return getDateString(departureTime, now);
	}
	public void setDepartureTimeString(String s)
	{
		this.departureTimeString = s;
	}
	
	public String toString()
	{
		return "Linje " + this.lineNumber + " til " + this.destinationName + " kl " + this.departureTimeString;
	}
	
}
